<template>
  <div class="siteLossArg">
    <div class="table-page-search-wrapper">
      <p-form layout="inline" @keyup.enter.native="searchQuery">
        <p-row :gutter="24">
          <p-col :xxl="5" :xl="6">
            <p-form-item label="耗损工具名称">
              <p-input placeholder="请输入内容" v-model="queryParamSelf.consumename"></p-input>
            </p-form-item>
          </p-col>
          <p-col :xxl="5" :xl="6">
            <p-form-item label="耗损类别">
              <p-input placeholder="请输入内容" v-model="queryParamSelf.consumetype"></p-input>
            </p-form-item>
          </p-col>
          <p-col :xxl="5" :xl="6">
            <span style="float: left;overflow: hidden;" class="table-page-search-submitButtons">
              <p-button type="primary" @click="searchQuerySelf" icon="search">查询</p-button>
              <p-button type="primary" @click="searchReset" icon="reload" style="margin-left: 8px">重置</p-button>
            </span>
          </p-col>
        </p-row>
      </p-form>
    </div>
    <!-- 操作按钮区域 -->
    <div class="table-operator">
      <p-button @click="handleAddSelf" type="primary" icon="plus">新增</p-button>
       <p-button :disabled="!selectedRowKeys.length > 0" @click="batchDelSkill" style="margin-left: 8px">
          <p-icon type="delete" />删除 
       </p-button>
    </div>
    <div class="ant-alert ant-alert-info" style="margin-bottom: 16px;">
      <i class="anticon anticon-info-circle ant-alert-icon"></i> 已选择 <a style="font-weight: 600">{{ selectedRowKeys.length }}</a>项
      <a style="margin-left: 24px" @click="onClearSelected">清空</a>

      <span style="float:right;">
        <a @click="handleExportXls('组件清单')"><p-icon type="download" />导出</a>
            <p-divider type="vertical" />
        <p-upload name="file" :showUploadList="false" :multiple="false" :headers="tokenHeader" :action="importExcelUrl" @change="handleImportExcel">
            <a><p-icon type="import" />导入</a>
        </p-upload>
        <p-divider type="vertical" />
          <p-popover title="自定义列" trigger="click" placement="leftBottom"  :overlay-style="getWidth">
          <template slot="content">
            <p-checkbox-group @change="onColSettingsChange" v-model="settingColumns" :defaultValue="settingColumns">
              <p-row>
                <template v-for="(item,index) in defColumns">
                  <template v-if="item.key!='rowIndex'&& item.dataIndex!='action'">
                      <p-col :span="12"><p-checkbox :value="item.dataIndex">{{ item.title }}</p-checkbox></p-col>
                  </template>
                </template>
              </p-row>
            </p-checkbox-group>
          </template>
          <a><p-icon type="setting" />自定义列</a>
        </p-popover>
      </span>
    </div>
    <p-table
      ref="table"
      size="middle"
      bordered
      rowKey="id"
      :columns="columns"
      :dataSource="options"
      :pagination="false"
      :loading="loading"
      :rowSelection="{fixed:true,selectedRowKeys: selectedRowKeys, onChange: onSelectChange}"
      @change="handleTableChange">
        <span slot="action" slot-scope="text, record, index">
          <a style="margin-right:10px" @click="editTable(record)">编辑</a>
          <p-popconfirm title="确定删除吗?" @confirm="() => handleDeleteSource(index)">
            <a>删除</a>
          </p-popconfirm>
        </span>
    </p-table>
    <siteLossArgModal ref="siteLossArgModal"/>
  </div>
</template>

<script>
import Vue from 'vue'
import JSuperQuery from '@/components/jeecg/JSuperQuery.vue'
import siteLossArgModal from '../modules/siteLossArgModal.vue'
import { JeecgListMixin } from '@/mixins/JeecgListMixin'
import mesPeProcess from "@/api/mesPeProcess";
import _ from "lodash";
export default {
  name: "siteComponentList",
  mixins:[JeecgListMixin],
  components: {
    JSuperQuery,
    siteLossArgModal,
  },
  props: {
    options: {
      type: Array,
      default: []
    },
    siteIndex: Object,
  },
  data() {
    return {
      searchQuery: '',
      queryParamSelf: {
        consumename: '',
        consumename: '',
        consumetype: '',
      },
      optionsCopy: [],
      importExcelUrl: '',
      // 表头
      columns: [
      ],
      //列设置
      settingColumns:[],
      //列定义
      defColumns: [
        {
          title: '序号',
          dataIndex: '',
          key:'rowIndex',
          width:60,
          align: 'center',
          customRender: function (t,r,index) {
            return parseInt(index)+1;
          }
        },
        {
          title: '工具名称',
          align: 'center',
          dataIndex: 'consumename'
        },
        {
          title: '耗损类别',
          align: 'center',
          dataIndex: 'consumetype'
        },
        {
          title: '损耗系数',
          align: 'center',
          dataIndex: 'coefficient'
        },
        {
          title: '耗损倍数',
          align: 'center',
          dataIndex: 'multiple'
        },
        {
          title: '操作',
          dataIndex: 'action',
          align: 'center',
          width:110,
          fixed: 'right',
          scopedSlots: { customRender: 'action' }
        }
      ],
    };
  },
  computed: {
  },
  created() {
    this.initColumns();
    this.optionsCopy = _.cloneDeep(this.options);
  },
  methods: {
    updateOption(){
      this.$store.commit('setCurNodeData', {
        ...this.siteIndex,
        options: this.options,
        field: 'consumeData',
      });
      this.optionsCopy = _.cloneDeep(this.options);
    },
    searchQuerySelf(){
      this.options = this.optionsCopy.filter( el =>{
        const consumename = this.queryParamSelf.consumename || el.consumename;
        const consumetype = this.queryParamSelf.consumetype || el.consumetype;
        return (el.consumename || '').includes(consumename) && (el.consumetype || '').includes(consumetype);
      });
    },
    searchReset(){
      this.queryParamSelf.consumename = ''; 
      this.queryParamSelf.consumetype = '';
      this.options = _.cloneDeep(this.optionsCopy);
    },
    editTable(row){
      this.$refs.siteLossArgModal.visible = true;
      this.$refs.siteLossArgModal.form = _.cloneDeep(row);
      this.$refs.siteLossArgModal.handle = 'edit';
    },
    handleAddSelf(){
      const obj = this.$refs.siteLossArgModal.form;
      for(const key of Object.keys(obj)){
        this.$refs.siteLossArgModal.form[key] = '';
      }
      this.$refs.siteLossArgModal.form['multiple'] = 1;
      this.$refs.siteLossArgModal.handle = 'add';
      this.$refs.siteLossArgModal.visible = true;
    },
    handleAddShkill(){
      this.$refs.tableChoose.visible = true;
    },
    getWidth:function(){
      if(this.defColumns.length > 14){
        return {width : '500px'}
      }
    },
    batchDelSkill(){
      this.$confirm({
        title: '确认删除',
        content: '是否删除选中数据?',
        onOk: function() {
          this.options = this.options.filter( el =>{
            return !this.selectedRowKeys.includes(el.id )
          });
          this.updateOption();
        }
      })
    },
    handleDeleteSource(index){
      this.options.splice(index, 1);
      this.updateOption();
    },
    //列设置更改事件
    onColSettingsChange (checkedValues) {
      var key = this.$route.name+":colsettings";
      Vue.ls.set(key, checkedValues, 7 * 24 * 60 * 60 * 1000)
      this.settingColumns = checkedValues;
      const cols = this.defColumns.filter(item => {
        if(item.key =='rowIndex'|| item.dataIndex=='action'){
          return true
        }
        if (this.settingColumns.includes(item.dataIndex)) {
          return true
        }
        return false
      })
      this.columns =  cols;
    },
    initColumns(){
      this.columns =  this.defColumns;
      this.settingColumns =  this.defColumns.map(function (item) {
        return item.dataIndex;
      });
    }
  },
};
</script>
<style scoped lang='less'>
.siteLossArg{
}
</style>